import re
import collections

def words_list(filename):
    try:
        with open(filename) as f_obj:
            content = f_obj.read()
    except FileNotFoundError:
        msg = "The file " + filename + " does not exist."
        print(msg)
    else:
        WORD_RE = re.compile(r'\W+')
        words = WORD_RE.split(content.lower())
        return words


def words_counter(filename):
    try:
        with open(filename) as f_obj:
            content = f_obj.read()
    except FileNotFoundError:
        msg = "The file " + filename + " does not exist."
        print(msg)
    else:
        words = words_list(filename)
        words_count = collections.Counter(words)
        return words_count



filename = 'input.txt'
words_count = words_counter(filename)
# 注意这个显示前10需要使用words_count.most_common(10)方法
for word, word_count in words_count.most_common(10):
    print('{0:<20} : {1}'.format(word, word_count))